package com.yanqu.road.dao.impl.monopolymarket.office;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.office.MonopolyMarketOfficeFinishData;
import com.yanqu.road.utils.date.DateHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2021/9/7 14:23
 */
public class MonopolyMarketOfficeFinishDaoImpl  extends TempDataObjectDao<MonopolyMarketOfficeFinishData> {
    public static String getMonopolyMarketOfficeFinishDataSql = "select * from `t_c_monopolymarket_office_finish` where `isSyn`=0 and `time` > ?";
    public Map<Long,MonopolyMarketOfficeFinishData> getMonopolyMarketOfficeFinishDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        int time = (int)(DateHelper.getCurrentSecond() - 30 * DateHelper.DAY_SECONDS);
        Map<Long,MonopolyMarketOfficeFinishData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketOfficeFinishDataSql);
            int i = 1;
            call.setInt(i++, time);
            rs = call.executeQuery();
            while (rs.next()) {
                MonopolyMarketOfficeFinishData item = parse(rs);
                map.put(item.getUserId(),item);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateMonopolyMarketOfficeFinishDataSql = "update `t_c_monopolymarket_office_finish` set userId=?,posIndex=?,level=?,no=?,robName=?,isSyn=?,time=? where id=?";
    @Override
    protected boolean updateData(MonopolyMarketOfficeFinishData monopolyMarketOfficeFinishData) {
        monopolyMarketOfficeFinishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketOfficeFinishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeFinishData.getPosIndex()));
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketOfficeFinishData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeFinishData.getNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketOfficeFinishData.getRobName()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketOfficeFinishData.getIsSyn()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeFinishData.getTime()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketOfficeFinishData.getId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketOfficeFinishDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketOfficeFinishDataSql = "insert into `t_c_monopolymarket_office_finish`(id,userId,posIndex,level,no,robName,isSyn,time) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketOfficeFinishData monopolyMarketOfficeFinishData) {
        monopolyMarketOfficeFinishData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketOfficeFinishData.getId()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketOfficeFinishData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeFinishData.getPosIndex()));
        para.put(i++, new DbParameter(Types.TINYINT, monopolyMarketOfficeFinishData.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeFinishData.getNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketOfficeFinishData.getRobName()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketOfficeFinishData.getIsSyn()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketOfficeFinishData.getTime()));
        if (getDb().executeNoneQuery(insertMonopolyMarketOfficeFinishDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketOfficeFinishData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketOfficeFinishData monopolyMarketOfficeFinishData = new MonopolyMarketOfficeFinishData();
        monopolyMarketOfficeFinishData.setId(rs.getLong("id"));
        monopolyMarketOfficeFinishData.setUserId(rs.getLong("userId"));
        monopolyMarketOfficeFinishData.setPosIndex(rs.getInt("posIndex"));
        monopolyMarketOfficeFinishData.setLevel(rs.getByte("level"));
        monopolyMarketOfficeFinishData.setNo(rs.getInt("no"));
        monopolyMarketOfficeFinishData.setRobName(rs.getString("robName"));
        monopolyMarketOfficeFinishData.setIsSyn(rs.getBoolean("isSyn"));
        monopolyMarketOfficeFinishData.setTime(rs.getInt("time"));

        return monopolyMarketOfficeFinishData;
    }

    public long getMaxId(){
        String sql = "select ifnull(max(id),0) as maxId from t_c_monopolymarket_office_finish";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }


}
